import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:json_to_dart/json_to_dart.dart';
import 'package:json_to_dart/util.dart';
import 'package:json_to_dart/widget/input_dialog.dart';
import 'package:toast/toast.dart';
import 'package:get/get.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Q1 - JsonToDart',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  /*
  * 提示
  * */
  static String tips =
      '提示：请先复制好Json或者注释，然后点击下面的按钮解析，解析生成dart后会自动复制到剪切板，直接去粘贴即可～';

  static String tipsFirst = '提示：使用本工具记得先导入covert，语句' + "import 'dart:convert';";

  /*
  * 结果
  * */
  static var result = ''.obs;

  /*
  * 控制器
  * */
  static TextEditingController controller =
      TextEditingController(text: "Sample");

  /*
  * 转Json
  * */
  void toJson(BuildContext context, [String codeStr]) async {
    final _class = controller.text;

    if (!strNoEmpty(_class)) {
      Toast.show('请输入类名', context);
      return;
    }
    // try {
      String _codeStr;
      if (strNoEmpty(codeStr)) {
        _codeStr = codeStr;
      } else {
        ClipboardData boardData = await Clipboard.getData(Clipboard.kTextPlain);
        _codeStr = boardData.text;
      }

      final classGenerator = new ModelGenerator(_class, false);
      DartCode dartCode = classGenerator.generateDartClasses(_codeStr);
      result.value = dartCode.code;
      Toast.show("转换成功，点击结果即可复制", context);
    // } catch (e) {
    //   Toast.show("请复制正确Json或注释", context);
    // }
  }

  /*
  * 复制
  * */
  void copyMethod(BuildContext context) {
    Toast.show("复制成功", context);
    Clipboard.setData(ClipboardData(text: result.value));
  }

  /*
  * 手动输入Json
  * */
  void inputJson(context) async {
    final _inputData = await inputDialog(context);
    if (!strNoEmpty(_inputData)) {
      return;
    }
    toJson(context, _inputData);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 30, vertical: 20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Obx(() => strNoEmpty(result.value) ? Container() : Spacer()),
            SelectableText(tipsFirst),
            SizedBox(height: 20),
            SelectableText(tips),
            SizedBox(height: 20),
            SizedBox(
              width: 200,
              child: Row(
                children: [
                  SelectableText('类名'),
                  SizedBox(width: 10),
                  Expanded(
                    child: TextField(controller: controller),
                  ),
                ],
              ),
            ),
            SizedBox(height: 30),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: () => toJson(context),
                  child: Text('剪切板解析'),
                ),
                Space(),
                ElevatedButton(
                  onPressed: () => inputJson(context),
                  child: Text('手动输入Json'),
                ),
                Space(),
                ElevatedButton(
                  onPressed: () {
                    print(int.parse('1').runtimeType);
                  },
                  child: Text('测试输出'),
                ),
              ],
            ),
            SizedBox(height: 30),
            Expanded(
              child: InkWell(
                child: Obx(
                  () => CupertinoScrollbar(
                    child: SingleChildScrollView(
                      child: Padding(
                        padding:
                            EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                        child: Text('${result.value}'),
                      ),
                    ),
                  ),
                ),
                onTap: () => copyMethod(context),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
